package com.starlight.flower_server.domain.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starlight.flower_server.controller.exception.BadRequestException;
import com.starlight.flower_server.controller.exception.NotFoundException;
import com.starlight.flower_server.domain.TotalData;
import com.starlight.flower_server.domain.item.cqrs.ItemPageQuery;
import com.starlight.flower_server.domain.item.cqrs.ItemPageQueryByCategoryId;
import com.starlight.flower_server.domain.item.data.ItemData;
import com.starlight.flower_server.infrastructure.Status;
import com.starlight.flower_server.infrastructure.entity.Item;
import com.starlight.flower_server.infrastructure.entity.ItemCategory;
import com.starlight.flower_server.infrastructure.entity.Order;
import com.starlight.flower_server.infrastructure.repository.ItemCategoryRepository;
import com.starlight.flower_server.infrastructure.repository.ItemRepository;
import com.starlight.flower_server.infrastructure.repository.OrderRepository;
import com.starlight.flower_server.domain.service.ItemService;
import com.starlight.flower_server.infrastructure.util.PageUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author yfy2001
 * @date 2023/4/5 15:08
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Resource
    private ItemRepository itemRepository;

    @Resource
    private OrderRepository orderRepository;

    @Resource
    private ItemCategoryRepository itemCategoryRepository;

    @Override
    public Item queryItemDetailById(Long id) {
        Item item=itemRepository.queryDetail(id);
        if (item==null){
            throw new NotFoundException(Status.RECORD_NOT_FOUND.getMessage(),Status.RECORD_NOT_FOUND);
        }
        return item;
    }

    @Override
    public Integer logicDeleteItemById(Long id) {
        Order order=new Order();
        order.setItemId(id);
        if (orderRepository.exists(order)){
            throw new BadRequestException(Status.ITEM_WITH_ORDER.getMessage(),Status.ITEM_WITH_ORDER);
        }
        Integer i=itemRepository.logicDelete(id);
        if (i.equals(0)){
            throw new NotFoundException(Status.RECORD_NOT_FOUND.getMessage(),Status.RECORD_NOT_FOUND);
        }
        return i;
    }

    @Override
    public Integer insertItem(Item item) {
        return itemRepository.insert(item);
    }

    @Override
    public Integer modifyItem(Item item) {
        if(item.getId()==null){
            throw new NotFoundException(Status.RECORD_NOT_FOUND.getMessage(),Status.RECORD_NOT_FOUND);
        }
        item.setModifier(null);
        item.setModified(new Date());
        return itemRepository.modify(item);
    }

    @Override
    public IPage<ItemData> queryItemList(ItemPageQuery query) {
        IPage<Item> page=itemRepository.queryPage(query);
        return page.convert(item->{
            ItemData itemData=new ItemData();
            BeanUtils.copyProperties(item,itemData);
            return itemData;
        });
    }

    @Override
    public TotalData<ItemData> queryAllItemData() {
        TotalData<ItemData> data=new TotalData<>();
        List<Item> itemList=itemRepository.queryAll();
        List<ItemData> itemDataList=itemList
                .stream().map(item -> {
                    ItemData itemData=new ItemData();
                    BeanUtils.copyProperties(item,itemData);
                    return itemData;
                }).toList();
        Long total=(long)itemDataList.size();
        data.setTable("Item");
        data.setStatTime(new Date());
        data.setTotal(total);
        data.setRecords(itemDataList);
        return data;
    }

    @Override
    public TotalData<ItemData> queryItemTotalCount() {
        TotalData<ItemData> data=new TotalData<>();
        Long total= itemRepository.queryTotalCount();
        data.setTable("Item");
        data.setStatTime(new Date());
        data.setTotal(total);
        return data;
    }

    @Override
    public List<ItemData> queryRandomItemList(Integer count) {
        List<Item> itemList=itemRepository.queryRandom(count);
        return itemList
                .stream().map(item -> {
                    ItemData itemData=new ItemData();
                    BeanUtils.copyProperties(item,itemData);
                    return itemData;
                }).toList();

    }

    @Override
    public List<ItemData> queryItemListByCategoryId(Long categoryId) {
        ItemCategory selectedItemCategory=itemCategoryRepository.queryDetail(categoryId);
        List<Item> itemList;
        List<Long> itemCategoryIdList=new ArrayList<>();
        if (selectedItemCategory.getParentId().equals(0L)){
            //如果是主分类则继续查询所有子分类商品
            List<ItemCategory> itemCategoryList=itemCategoryRepository.querySub(categoryId);
            itemCategoryIdList=itemCategoryList
                    .stream().map(ItemCategory::getId).toList();
        }else {
            itemCategoryIdList.add(categoryId);
        }
        itemList=itemRepository.queryByCategory(itemCategoryIdList);
        return itemList
                .stream().map(item -> {
                    ItemData itemData=new ItemData();
                    BeanUtils.copyProperties(item,itemData);
                    return itemData;
                }).toList();
    }

    @Override
    public IPage<ItemData> queryItemPageByCategoryId(ItemPageQueryByCategoryId query) {
        Long categoryId=query.getCategoryId();
        ItemCategory selectedItemCategory=itemCategoryRepository.queryDetail(categoryId);
        List<Long> itemCategoryIdList=new ArrayList<>();
        if (selectedItemCategory.getParentId().equals(0L)){
            //如果是主分类则继续查询所有子分类商品
            List<ItemCategory> itemCategoryList=itemCategoryRepository.querySub(categoryId);
            itemCategoryIdList=itemCategoryList
                    .stream().map(ItemCategory::getId).toList();
        }else {
            itemCategoryIdList.add(categoryId);
        }
        IPage<Item> page=itemRepository.queryPageByCategory(itemCategoryIdList,query);
        return page.convert(item->{
            ItemData itemData=new ItemData();
            BeanUtils.copyProperties(item,itemData);
            return itemData;
        });
    }


}
